﻿using System.Linq;

using VSTalk.Engine.Core.Context;
using VSTalk.Engine.Core.XmppConnection;
using VSTalk.Model;

namespace VSTalk.Engine.Core.XmppCommunication
{
    public static class CommunicationServiceFactory
    {
        public static T Create<T>(Interlocutor interlocutor) where T : BaseCommunicationService, new()
        {
            var targetClient = AppServices.Get<IModelContext>().Clients
                    .First(client => client.Contacts.Contains(interlocutor));
            return Create<T>(targetClient);
        }

        public static T Create<T>(Room room) where T : BaseCommunicationService, new()
        {
            var targetClient = AppServices.Get<IModelContext>().Clients
                    .First(client => client.Rooms.Contains(room));
            return Create<T>(targetClient);
        }

        public static T Create<T>(Client client) where T : BaseCommunicationService, new()
        {
            var connection = AppServices.Get<IConnectionManager>().GetOrCreateConnection(client);
            var service = new T();
            service.Initialize(connection);
            return service;
        }
    }
}